from rest_framework import serializers
from .models import (
    EvaluationAnalysis, 
    ImprovementSuggestion, 
    TrainingRecommendation, 
    DriverImprovementMemory
)
from order.models import OrderRating, Order

class OrderSerializer(serializers.ModelSerializer):
    """订单序列化器"""
    class Meta:
        model = Order
        fields = ['id', 'order_number', 'start_address', 'end_address', 'order_amount', 'create_time']

class OrderRatingSerializer(serializers.ModelSerializer):
    """订单评价序列化器"""
    order = OrderSerializer(read_only=True)
    
    class Meta:
        model = OrderRating
        fields = ['id', 'order', 'passenger_id', 'driver_id', 'rating', 'comment', 'rating_time']

class EvaluationAnalysisSerializer(serializers.ModelSerializer):
    """评价分析序列化器"""
    order_rating = OrderRatingSerializer(read_only=True)
    analysis_result = serializers.SerializerMethodField()
    
    class Meta:
        model = EvaluationAnalysis
        fields = '__all__'
    
    def get_analysis_result(self, obj):
        """生成分析结果摘要"""
        result_parts = []
        
        # 添加情感分析结果
        if obj.sentiment_label:
            result_parts.append(f"情感: {obj.sentiment_label}")
        
        # 添加关键短语
        if obj.key_phrases and len(obj.key_phrases) > 0:
            try:
                # 安全处理关键短语
                phrases = []
                for phrase in obj.key_phrases[:3]:
                    if isinstance(phrase, dict):
                        phrases.append(phrase.get('phrase', '关键词'))
                    else:
                        phrases.append(str(phrase))
                if phrases:
                    result_parts.append(f"关键词: {', '.join(phrases)}")
            except Exception as e:
                print(f"处理关键短语时出错: {e}")
        
        # 添加改进领域
        if obj.improvement_areas and len(obj.improvement_areas) > 0:
            try:
                # 安全处理改进领域
                areas = []
                for area in obj.improvement_areas[:2]:
                    if isinstance(area, dict):
                        area_name = area.get('area', '未知领域')
                        areas.append(area_name)
                    else:
                        areas.append(str(area))
                if areas:
                    result_parts.append(f"改进: {', '.join(areas)}")
            except Exception as e:
                print(f"处理改进领域时出错: {e}")
        
        return ' | '.join(result_parts) if result_parts else '暂无分析结果'

class ImprovementSuggestionSerializer(serializers.ModelSerializer):
    """改进建议序列化器"""
    analysis = EvaluationAnalysisSerializer(read_only=True)
    
    class Meta:
        model = ImprovementSuggestion
        fields = '__all__'

class TrainingRecommendationSerializer(serializers.ModelSerializer):
    """培训建议序列化器"""
    
    class Meta:
        model = TrainingRecommendation
        fields = '__all__'

class DriverImprovementMemorySerializer(serializers.ModelSerializer):
    """司机改进记忆序列化器"""
    
    class Meta:
        model = DriverImprovementMemory
        fields = '__all__' 